package wdrun

import (
	"fmt"
	"gitee.com/yutiandou/wdtools/wdevent"
)

func help(ctx *wdevent.Context) error {
	args := GetArgsFromContext(ctx)
	cmds := GetCmdFromContext(ctx)
	global := GetGlobalParameterFromContext(ctx)
	show := false
	if _, ok := args.kv["h"]; ok {
		show = true
	}
	if _, ok := args.kv["help"]; ok {
		show = true
	}
	if _, ok := args.cmd["help"]; ok {
		show = true
	}
	if len(args.cmd) == 0 {
		show = true
	}
	if show {
		showCmd(global, cmds, args)
	}
	return nil
}
func showCmd(g []*Parameter, cs []*Cmd, a *Args) {
	fmt.Println("application:", a.desc)
	fmt.Println("enter parameters in the following format:")
	fmt.Println("[application] [global option..] [cmd] [order option..]")
	//fmt.Println("The global configuration is as follows")
	fmt.Println("the following information is displayed:")
	for _, i := range g {
		fmt.Println("    -", i.key, "    default:", i.value, "    help:", i.help)
	}
	fmt.Println("    - h    default:nil    help:display help information")
	for _, i := range cs {
		fmt.Println(i.cmd, "   ", i.desc)
		for _, j := range i.args {
			fmt.Println("    -", j.key, "    default:", j.value, "    help:", j.help)
		}
	}
}
